package com.zuoyue.myhttprequest;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.JsonObject;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String url = "";
    private Button sendPostNoParam;
    private Button sendPostParam;
    private TextView showData;
    private Button sendJsonPostParam;
    private Button sendGetNoParam;
    private Button sendGetParam;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        initView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(String data) {
        showData.setText(data);
    }

    private void initView() {
        sendPostNoParam = (Button) findViewById(R.id.sendPostNoParam);
        sendPostParam = (Button) findViewById(R.id.sendPostParam);
        showData = (TextView) findViewById(R.id.showData);

        sendPostNoParam.setOnClickListener(this);
        sendPostParam.setOnClickListener(this);
        sendJsonPostParam = (Button) findViewById(R.id.sendJsonPostParam);
        sendJsonPostParam.setOnClickListener(this);
        sendGetNoParam = (Button) findViewById(R.id.sendGetNoParam);
        sendGetNoParam.setOnClickListener(this);
        sendGetParam = (Button) findViewById(R.id.sendGetParam);
        sendGetParam.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sendPostNoParam:
                RequestBody postNoParamUser = new FormBody.Builder()
                        .build();
                HttpUtil.sendRequest(this, "/postNoParamUser",postNoParamUser);
                break;
            case R.id.sendPostParam:
                RequestBody body = new FormBody.Builder()
                        .add("id", "222")
                        .build();
                HttpUtil.sendRequest(this, "/postParamUser", body);
                break;
            case R.id.sendJsonPostParam:
                JSONObject jsonObject = new JSONObject ();
                try {
                    jsonObject.put("id",1111111);
                    jsonObject.put("name","顺");
                    jsonObject.put("studentId","123123");
                    jsonObject.put("sex","小萌新");
                    jsonObject.put("data","I have pan");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                RequestBody requestBody = FormBody.create(MediaType.parse("application/json;charset=utf8"),
                                            jsonObject.toString());
                HttpUtil.sendRequest(this, "/postObjectParamUser", requestBody);
                break;
            case R.id.sendGetNoParam:
                HttpUtil.sendRequest(this, "/getUser", null);
                break;
            case R.id.sendGetParam:
                HttpUtil.sendRequest(this, "/getParamUser?id=123321&name=xxx", null);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();

    }
}